﻿using AppDataStruct;
using ComForm;
using ComForm.Auxiliary;
using ComUIControl;
using EECMISWS;
using KCommon;
using KeyboardArg;
using KWindow.KPageCore;
using LogLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace YSLPages
{
    public partial class PageFillContactPhone : PageControlBase
    {


        private PreApplyInfo _appInfo = null;

        //private SFZInfo _sfz = new SFZInfo();


        private TextBox textBoxContactNumber = null;



        //private CodeMapService _mapService = null;
        //private bool _bInited = false;

        public PageFillContactPhone()
        {
            InitializeComponent();
            SetProcessBase(ProcessFillContactPhone.Instance);
        }

        private void GetControls()
        {
            textBoxContactNumber = GetControl<TextBox>("textBoxContactNumber") as TextBox;           
        }

        //private void InitCodeMapService()
        //{
        //    _mapService = CodeMapService.Instance;
        //    if (!_mapService.Inited)
        //    {
        //        _mapService.LoadConfig();
        //    }
        //}

        //初始化窗体数据
        public override void InitCtrls()
        {
            InitKeyboards();
            base.InitCtrls();
            GetControls();
            //InitCodeMapService();
        }

        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            //_bInited = false;
            _appInfo = Process.GetValue<PreApplyInfo>("PreApplyInfo");           
            //_sfz = Process.GetValue<SFZInfo>("SFZInfo");           

            InitTextBoxs();
            //_bInited = true;
        }
        

        protected void InitTextBoxs()
        {
            try
            {
                if (_appInfo == null)
                {
                    Log.WriteAsync("申请查询的信息为空");
                    return;
                }                

                Log.WriteAsync($"apply info:{KCommon.JsonHandler.ConvertToJsonString<PreApplyInfo>(_appInfo)}");
                //Log.WriteAsync($"sfz info:{KCommon.JsonHandler.ConvertToJsonString<SFZInfo>(_sfz)}");
                Log.WriteAsync($"TextBoxs数量：{TextBoxs.Count}");
                Type type = _appInfo.GetType();
                string FieldName = "";
                foreach (TextBox tb in TextBoxs)
                {
                    Log.WriteAsync("TextBoxName:" + tb.Name);
                    if (!TextBoxBindings.ContainsKey(tb.Name))
                    {
                        continue;
                    }
                    FieldName = TextBoxBindings[tb.Name].Key;
                    Log.WriteAsync("TextBox:" + TextBoxBindings[tb.Name].Key);
                    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(FieldName);
                    if (propertyInfo != null)
                    {
                        if (propertyInfo.PropertyType == typeof(CodeMap))
                        {
                            CodeMap cm = (CodeMap)propertyInfo.GetValue(_appInfo, null);
                            tb.Text = cm.Value; //给对应属性赋值
                            textMapService[FieldName] = cm.Value;
                            Log.WriteAsync("CodeMap,Code:" + cm.Code + ",Value:" + cm.Value);
                        }
                        else
                        {
                            tb.Text = (string)propertyInfo.GetValue(_appInfo, null); //给对应属性赋值
                        }
                        Log.WriteAsync("文本框" + tb.Name + "：" + tb.Text);
                    }
                    else
                    {
                        Log.WriteAsync("找不到名称为[" + FieldName + "]属性.");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
            }

        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            // 更新关键字/键值索引字典
            textMapService.UpdateMapDic();
            if (_appInfo == null)
                return;
            Type type = _appInfo.GetType();
            string FieldName = "";
            string FieldValue = "";
            foreach (TextBox tb in TextBoxs)
            {
                if (!TextBoxBindings.ContainsKey(tb.Name))
                {
                    continue;
                }
                FieldName = TextBoxBindings[tb.Name].Key;
                FieldValue = tb.Text.Trim();
                System.Reflection.PropertyInfo propertyInfo = type.GetProperty(FieldName);
                if (propertyInfo != null)
                {                    
                    propertyInfo.SetValue(_appInfo, FieldValue, null); //给对应属性赋值
                    Log.WriteAsync($"（{propertyInfo.Name}） value:{FieldValue}");                   
                }
                else
                {
                    Log.WriteAsync("找不到名称为[" + FieldName + "]属性.");
                }
            }            

            Process.SetValue("PreApplyInfo", _appInfo);
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            _appInfo = null;
            //_sfz = null;
            foreach (TextBox tb in TextBoxs)
            {
                tb.Text = "";
            }
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
            CheckFillingIsCompleted();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("倒计时结束，未能完善个人信息，中止申请流程.");
            base.OnCountdownCompleted(sender, e);
        }

        protected override void btnBack_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnBack_Click(sender, e);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            if (!CheckAllInfoIsFilling())
                return;
            StopCountdown();
            base.btnNext_Click(sender, e);
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            StopCountdown();
            base.btnCancel_Click(sender, e);
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case (int)MessageType.WM_COPYDATA:
                    OnKeyBoardMsgReceived(m);
                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

        protected bool CheckAllInfoIsFilling()
        {
            Type type = _appInfo.GetType();
            foreach (TextBox tb in TextBoxs)
            {
                if (!TextBoxBindings.ContainsKey(tb.Name))
                {
                    continue;
                }

                KeyBoardArgs args = TextBoxBindings[tb.Name];
                

                if (!string.IsNullOrEmpty(tb.Text))
                {
                    System.Reflection.PropertyInfo propertyInfo = type.GetProperty(args.Key);
                    continue;
                }

                if (args.NotNull)
                {
                    string msg = string.Format("选项[{0}]不能为空，请填写。", args.Title);
                    MsgBox.Show(msg);
                    return false;
                }
            }
            
            return true;
        }        
    }
}
